package com.thrd.taiexhibition.model;

import com.alibaba.fastjson.JSON;
import com.thrd.basicframework.http.OkHttpUtils;
import com.thrd.basicframework.http.callback.ThrdCallBack;
import com.thrd.taiexhibition.bean.BeanCity;
import com.thrd.taiexhibition.bean.BeanCityLetter;
import com.thrd.taiexhibition.bean.BeanCityList;
import com.thrd.taiexhibition.bean.BeanSystemInfo;
import com.thrd.taiexhibition.callback.MvpCallback;
import com.thrd.taiexhibition.utils.Constant;
import com.thrd.taiexhibition.utils.LetterUtil;
import com.thrd.taiexhibition.utils.SortTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ${唐杰}
 * @date: 2021/4/25
 */
public class MainModel {

    public static final String TAG="MainModel";


    /**
     * 获取初始数据
     */
    public void getSystemInfo(final MvpCallback<BeanSystemInfo> mvpCallback) {
        OkHttpUtils.get().url(Constant.HTTP_SYSTEM_INFO)
                .tag(TAG)
                .build().execute(new ThrdCallBack<BeanSystemInfo>() {
            @Override
            public void onFailed(String e, int code) {
                mvpCallback.onFailed(e, code);
            }

            @Override
            public void onResponse(BeanSystemInfo response, int id) {
                mvpCallback.onSuccess(response);
            }

            @Override
            public BeanSystemInfo onJson(String s) {
                //BeanCityList
                BeanSystemInfo mBeanSystemInfo =JSON.parseObject(s, BeanSystemInfo.class);
                List<BeanCity> listBean = mBeanSystemInfo.getCityList();
                BeanCityList mBeanCityList=new BeanCityList();
                if(listBean!=null){
                    StringBuffer stringBuffer=new StringBuffer();
                    Map<String, BeanCityLetter> mapBean=new HashMap<>();
                    for (BeanCity beanCity:listBean){
                        String letter= LetterUtil.getFirstLetter(beanCity.getCityName());
                        if(mapBean.containsKey(letter)){
                            //map集合中已有该字母集合，继续添加
                            BeanCityLetter beanCityLetter =mapBean.get(letter);
                            beanCityLetter.getCityList().add(beanCity);
                        }else{
                            //map集合中没有该字母集合，第一次添加
                            BeanCityLetter beanCityLetter =new BeanCityLetter();
                            beanCityLetter.setLetter(letter);
                            beanCityLetter.getCityList().add(beanCity);
                            mapBean.put(letter,beanCityLetter);

                        }
                    }

                    List<BeanCityLetter> list = new ArrayList<BeanCityLetter>(mapBean.values());
                    SortTool.sortLetter(list);
                    for (BeanCityLetter beanCityLetter:list){
                        stringBuffer.append(beanCityLetter.getLetter());
                    }
                    mBeanCityList.setListBeanCityLetters(list);
                    mBeanCityList.setLettersList(stringBuffer.toString());

                }
                mBeanSystemInfo.setBeanCityLists(mBeanCityList);
                return mBeanSystemInfo;
            }


        });
    }

}
